<?php
namespace  Backend\Controller;
use Think\Controller;
/**
 * Created by PhpStorm.
 * User: whs
 * Date: 2017/4/17
 * Time: 9:45
 */

class AdController extends BaseController
{

    protected $ad_model;
    protected $ad_game_model;

    public function __construct() {
        parent::__construct();
        $this->ad_model = D('Ad');
        $this->ad_game_model = D('AdGame');
    }

    public function index() {
        $data = $this->ad_model->get_ad();

        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));

        $this->display();
    }

    public function create() {
        $data = $_POST;
        if($data) {
            if (!$data = $this->ad_model->create()) {
                $this->ajaxReturn(['status' => 'failed', 'msg' => $this->ad_model->getError()]);
            } else {
                if($result = $this->ad_model->add()) {
                    $this->ajaxReturn(['status' => 'success', 'msg' => '添加成功！']);
                } else {
                    $this->ajaxReturn(['status' => 'failed', 'msg' => $this->ad_model->getError() ]);
                }
            }
        }

        $game = $this->ad_game_model->field('id,game_name,g_id')->order('id desc')->select();
        $this->assign('game', $game);
        $this->display();
    }

    public function edit() {
        $id = I('get.id');
        $data   = $_POST;
        $model = D('Ad');
        if($data) {

            if (!$model->create()) {
                $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError() ]);
            }else{
                if($model->save() !== FALSE) {
                    $this->ajaxReturn(['status' => 'success', 'msg' => '修改成功！']);
                    exit;
                }else{
                    $this->ajaxReturn(['status' => 'failed', 'msg' => $model->getError() ]);
                    exit;
                }
            }
        }

        $where['id'] = ['eq', $id];
        $ad = $this->ad_model->where($where)->find();

        $game_choose = explode(',', trim($ad['game_id'], ','));
        $game_count = count($game_choose);  //已选择游戏的物料

        $game = $this->ad_game_model->field('id,game_name,g_id')->order('id desc')->select();

        $cgame = []; //定义已选择的游戏物料

        if ($game_count) {

            foreach ($game_choose as $k1 => $v1) {
                foreach ($game as $k2 => $v2) {
                    if ($v1 == $v2['id']) {
                        $cgame[] = $game[$k2];
                        unset($game[$k2]);
                    }
                }
            }
        }

        $this->assign('cgame', $cgame);
        $this->assign('game',$game);
        $this->assign('ad', $ad);
        $this->display();
    }

    public function delete() {

        if(!$_POST['id']) {
            $this->ajaxReturn(['status' => 'failed', 'msg' => '操作有误，重新选择！']);
        }

        $where  = 'id in('.trim($_POST['id'],',').')';
        $res    = $this->ad_model->where($where)->delete();
        if ($res) {
            $this->ajaxReturn(['status' => 'success', 'msg' => '操作成功！']);
        } else {
            $this->ajaxReturn(['status' => 'failed', 'msg' => '操作失败！']);
        }
    }
}

